package com.example.mall.mq;

import com.alibaba.fastjson.JSON;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.Products;
import com.example.mall.dto.EmailSend2MQ;
import com.example.mall.dto.SMSSend2MQ;
import com.example.mall.dto.StationMessageSend2MQ;
import com.example.mall.prop.ProductMessageProp;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author: xxxliu
 * @CreateTime: 2025-06-06
 * @Version: 1.0
 */
@Component
@Slf4j
public class ProductMessageSender {

    @Autowired
    private ProductMessageProp productMessageProp;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //商品发布，消息推送
    public void publishMessage(Products products) {
        //1.短信推送
        //消息内容
        String smsContent = String.format(productMessageProp.getSmsContent(), products.getProductId(),products.getName());
        log.info("商品服务，推送短信消息 {}",smsContent);
        //手机号
        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_SMS_NAME,
                JSON.toJSONString( new SMSSend2MQ(Arrays.asList(1L),"商品发布",smsContent ))
        );


        rocketMQTemplate.asyncSend(
                BaseConstants.MessageMQ.TOPIC_MESSAGE + ":" + BaseConstants.MessageMQ.TAGS_SMS_NAME,
                JSON.toJSONString(new SMSSend2MQ(Arrays.asList(1L), "商品发布", smsContent)), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(sendResult);
                    }

                    @Override
                    public void onException(Throwable e) {
                        e.printStackTrace();
                    }
                }
        );

        //2.邮件推送
        String emailContent = String.format(productMessageProp.getEmailContent(), products.getProductId(),products.getName());

        log.info("商品服务，推送邮件消息 {}",emailContent);

        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_EMAIL_NAME,
                JSON.toJSONString( new EmailSend2MQ(Arrays.asList(1L),"商品发布",emailContent ))
        );

        //3.站内信推送
        String stationContent = String.format(productMessageProp.getStationContent(), products.getProductId(),products.getName());
        log.info("商品服务，推送站内信消息 {}",stationContent);

        Message<String> message = MessageBuilder.withPayload(
                JSON.toJSONString(new StationMessageSend2MQ("商品发布啦", stationContent, Arrays.asList(1L, 2L), "系统消息"))).build();

        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_STATION_MESSAGE,
                message);
    }
}
